/**
 * Auto build script.
 * @author TZ <liuyong3@ucweb.com>
 * 
 * Usage:
 *   1. install [nodejs](http://nodejs.org)
 *   2. run `npm install -g grunt-cli` to install grunt
 *   3. go to root path then run `npm install`
 *   4. run `grunt` to build
 *   5. see also: [gruntjs](http://gruntjs.com/)
 */
module.exports = function(grunt) {
  //Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    gameId: '<%=pkg.name%>',
    apkName: '<%=gameId%>.apk',
    debugApkName: 'daotahero-debug',

    //define path 
    distDir: '<%=process.cwd()%>/dist',
    toolsDir: '<%=process.cwd()%>/tools',

    //Task: clean dist folder (Note: when open window explorer in this dir, it will fail.)
    clean: {
      options: {
        force: true
      },
      assets: ["!dist/assets", "dist/*","dist/assets/**"]
    },

    mkdir: {
      assets: {
        create: ['dist/assets']
      }
    },

    //Task: copy all file except *.js & lib , then copy *.run.js
    copy: {
      html: {
        files: [
          {expand: true, cwd:'src/', src: ['**','!**/**.js','!9game/libs/**'], dest: 'dist/assets/html/'},
          {expand: true, cwd:'src/', src: ['**/run/**.run.js'], dest: 'dist/assets/html/'}
        ]
      }
    },

    //Task: compression js
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      html: {
        files: [
          //uglify all *.run.js
          {expand: true, cwd: 'src/', src: ['**/run/*.js'], dest: 'dist/assets/html/', ext: '.run.js'}
        ]
      }
    },

    //zip
    compress: {
      html: {
        options: {
          mode: 'zip',
          archive: 'dist/assets/html.zip'
        },
        files: [
          {expand: true, src: ['**'], cwd: 'dist/assets/html/', dest: ''},
        ]
      }
    },

    //Task: exec shell
    exec: {
      //repack the apk
      repack: {
        command: [
          'cd tools',
          'java -jar apktool.jar d -r -s -f ../tools/<%=debugApkName%>.apk ../dist/<%=debugApkName%>',
          'java -jar apktool.jar b -f ../dist/<%=debugApkName%> ../dist/<%=apkName%>',
          'cd ../dist && rd <%=debugApkName%> /s/q'
        ].join('&&')
      },

      //zip html.zip
      zip: {
        command: [
          'cd dist/assets/ && del html.zip /s/q && cd html',
          '<%=toolsDir%>/7za a -tzip ../html.zip -r ./*'
       ].join('&&')
      },

      //pack jar && apk
      apk: {
        command: [
          'cd dist',
          //'copy /Y <%=debugApkName%>.apk <%=apkName%>',
          '<%=toolsDir%>/aapt remove -v <%=apkName%> "assets/html.zip"',
          '<%=toolsDir%>/aapt add -v <%=apkName%> "assets/html.zip"',
          'jarsigner -verbose -sigalg MD5withRSA -digestalg SHA1 -keystore <%=toolsDir%>/test.keystore -storepass 123456 <%=apkName%> test'
        ].join('&&')
      },

      //install to phone
      install: {
        command: [
          'cd dist',
          'adb devices',
          'echo ^> adb uninstall <%=gameId%>',
          'adb uninstall <%=gameId%>',
          'echo ^> adb install <%=apkName%>',
          'adb install <%=apkName%>',
          'echo ^> adb shell am start -n "<%=gameId%>/com.uc.daotaheroassistant.UserGuideActivity"',
          'adb shell am start -n "<%=gameId%>/com.uc.daotaheroassistant.UserGuideActivity"'
        ].join('&&')
      }
    }
  });

  //Load the plugin
  grunt.loadNpmTasks('grunt-mkdir');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-compress');
  grunt.loadNpmTasks('grunt-exec');

  //Default task(s).
  // grunt.registerTask('default', ['clean','copy:html','compress:html','exec:apk','exec:install']);
  grunt.registerTask('default', ['clean','mkdir','copy:html','exec:zip','exec:repack','exec:apk','exec:install']);
  grunt.registerTask('publish', ['clean','mkdir','copy:html','uglify','exec:zip','exec:repack','exec:apk','exec:install']);
  grunt.registerTask('html', ['clean','mkdir','copy:html','exec:zip']);
  grunt.registerTask('html-min', ['clean','mkdir','copy:html','uglify','exec:zip']);
};